#include "SDL/SDL.h"
#include "OGL.h"
#include "Attributes.h"
#include "Graphics.h"
#include "Game.h"
#include <iostream>

#define DEBUG false||DEBUG_ALL

Graphics::Graphics(Game *game,int argc, char* args[])
{
/*	glutInit( &argc, args );
	 //Create OpenGL 2.1 context
        glutInitContextVersion( 2, 1 );

        //Create Double Buffered Window
        glutInitDisplayMode( GLUT_DOUBLE );
        glutInitWindowSize( SCREENWIDTH, SCREENHEIGHT );
        glutCreateWindow( "OpenGL" );
*/
	if(!this->init())
        {
                std::cout<<"failed init of graphics.\n";
        }
        else
        {
                if(DEBUG)
                std::cout<<"graphics ok\n";
        }


}

Graphics::~Graphics()
{

}


bool Graphics::init()
{

       
     	if( SDL_Init(SDL_INIT_EVERYTHING)==-1)
        {
                std::cout<<"sdl init fail\n";
                return false;
        }
 
     
    
   
        screen = SDL_SetVideoMode( SCREENWIDTH, SCREENHEIGHT, SCREENBPP, SDL_OPENGL );
        if(this->screen==NULL)
        {
                std::cout<<"screen null\n";
                return false;
        }
	//Initialize OpenGL 
	if( initGL() == false ) 
	{
		std::cout<<"error with opengl\n";
		return false; 
	}
	
  
        return true;
}

bool Graphics::initGL()
{
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	glClearColor( 0.f, 0.f, 0.f, 1.f );
	GLenum error=glGetError();
	if(error!=GL_NO_ERROR)
	{
		std::cout<<"error with open gl init\n";
		return false;
	}
	return true;
}

void Graphics::render()
{
	//Clear color buffer
	glClear( GL_COLOR_BUFFER_BIT );

	//Render quad
	glBegin( GL_QUADS );
	glVertex2f( -0.5f, -0.5f );
	glVertex2f(  0.5f, -0.5f );
	glVertex2f(  0.5f,  0.5f );
	glVertex2f( -0.5f,  0.5f );
	glEnd();

	//Update screen
	SDL_GL_SwapBuffers();
}

